package estore.model;
import java.util.Objects;
import java.io.Serializable;

/**
 * 商品实体类
 */
public class Product4 implements Comparable<Product4>, Serializable {
    private static final long serialVersionUID = 1L;

    private String id;      // 商品ID
    private String name;    // 商品名称
    private double price;   // 价格
    private int stock;      // 库存

    // 添加无参构造函数
    public Product4() {
    }

    public Product4(String id, String name, double price, int stock) {
        this.id = id;
        this.name = name;
        this.price = price;
        this.stock = stock;
    }

    // Getters and Setters
    public String getId() {
        return id;
    }

    public void setId(String id) {
        this.id = id;
    }

    public String getName() {
        return name;
    }

    public void setName(String name) {
        this.name = name;
    }

    public double getPrice() {
        return price;
    }

    public void setPrice(double price) {
        // 添加价格验证
        if (price < 0) {
            throw new IllegalArgumentException("价格不能为负数");
        }
        this.price = price;
    }

    public int getStock() {
        return stock;
    }

    public void setStock(int stock) {
        // 添加库存验证
        if (stock < 0) {
            throw new IllegalArgumentException("库存不能为负数");
        }
        this.stock = stock;
    }

    @Override
    public String toString() {
        return String.format("ID: %-5s | 名称: %-20s | 价格: ¥%-8.2f | 库存: %d", id, name, price, stock);
    }

    // 重写 equals 和 hashCode 是一个好习惯，特别是当对象要放入Set或作为Map的key时
    @Override
    public boolean equals(Object o) {
        if (this == o) return true;
        if (o == null || getClass() != o.getClass()) return false;
        Product4 product = (Product4) o;
        return Objects.equals(id, product.id);
    }

    @Override
    public int hashCode() {
        return Objects.hash(id);
    }

    // 取消注释，实现Comparable接口，按ID排序
    @Override
    public int compareTo(Product4 other) {
        if (this.id == null && other.id == null) {
            return 0;
        }
        if (this.id == null) {
            return -1;
        }
        if (other.id == null) {
            return 1;
        }
        return this.id.compareTo(other.id);
    }

    // 添加业务逻辑方法

    /**
     * 减少库存
     * @param quantity 减少的数量
     * @return 是否操作成功
     */
    public boolean decreaseStock(int quantity) {
        if (quantity <= 0) {
            throw new IllegalArgumentException("减少数量必须大于0");
        }
        if (this.stock < quantity) {
            return false; // 库存不足
        }
        this.stock -= quantity;
        return true;
    }

    /**
     * 增加库存
     * @param quantity 增加的数量
     */
    public void increaseStock(int quantity) {
        if (quantity <= 0) {
            throw new IllegalArgumentException("增加数量必须大于0");
        }
        this.stock += quantity;
    }

    /**
     * 检查是否有库存
     * @return 是否有库存
     */
    public boolean isInStock() {
        return this.stock > 0;
    }

    /**
     * 检查库存是否足够
     * @param quantity 需要的数量
     * @return 是否足够
     */
    public boolean hasSufficientStock(int quantity) {
        return quantity > 0 && this.stock >= quantity;
    }

    /**
     * 获取商品信息摘要
     * @return 简化的商品信息
     */
    public String getSummary() {
        return String.format("%s (¥%.2f)", name, price);
    }

    /**
     * 应用折扣
     * @param discountRate 折扣率 (0-1之间，如0.1表示9折)
     */
    public void applyDiscount(double discountRate) {
        if (discountRate < 0 || discountRate > 1) {
            throw new IllegalArgumentException("折扣率必须在0-1之间");
        }
        this.price = this.price * (1 - discountRate);
    }

    /**
     * 复制商品对象
     * @return 新的商品对象
     */
    public Product4 copy() {
        return new Product4(this.id, this.name, this.price, this.stock);
    }
}